<?php

class CustomerAction extends CommonAction {

    // 查询数据
    public function index() {
        //$Customer = M("Customer");
        //$list = $Customer->limit(3)->order('id desc')->select();
        //$this->list =  $list;
        $this->display();
    }
	
	 public function indexjson() {
        $Customer = M("Customer");
        $list = $Customer->limit(3)->order('id desc')->select();
		//echo $Customer->getLastSql();
		//dump($list);exit;
		$i = 0;
		foreach($list AS $row ){
			$customer[$i]['table_id'] = $row['id'];
			$customer[$i]['table_title'] = $row['title'];
			$customer[$i]['table_shorttitle'] = $row['shorttitle'];
			$customer[$i]['table_address'] = $row['address'];
			$customer[$i]['table_province']= $row['province'];
			$customer[$i]['table_contact'] = $row['contact'];
			$customer[$i]['table_phone'] = $row['phone'];
			$customer[$i]['table_mobile'] = $row['mobile'];
			$customer[$i]['table_email'] = $row['email'];
			$customer[$i]['table_status'] = $row['status'];
			$customer[$i]['table_typeid'] = $row['typeid'];			
		
		$i++;
		}
		
        echo json_encode($customer);
        
    }

    // 写入数据
    public function insert() {
        $Customer = D("Customer");
        if ($vo = $Customer->create()) {
            $list = $Customer->add();
            if ($list !== false) {
                $this->success('数据保存成功！',U('Index/index'));
            } else {
                $this->error('数据写入错误！');
            }
        } else {
            $this->error($Customer->getError());
        }
    }

    // 更新数据
    public function update() {
        $Customer = D("Customer");
        if ($vo = $Customer->create()) {
            $list = $Customer->save();
            if ($list !== false) {
                $this->success('数据更新成功！',U('Index/index'));
            } else {
                $this->error("没有更新任何数据!");
            }
        } else {
            $this->error($Customer->getError());
        }
    }

    // 删除数据
    public function delete($id) {
        if (!empty($id)) {
            $Customer = M("Customer");
            $result = $Customer->delete($id);
            if (false !== $result) {
                $this->success('删除成功！');
            } else {
                $this->error('删除出错！');
            }
        } else {
            $this->error('ID错误！');
        }
    }

    // 编辑数据
    public function edit($id) {
        if (!empty($id)) {
            $Customer = M("Customer");
            $vo = $Customer->getById($id);
            if ($vo) {
                $this->vo   =   $vo;
                $this->display();
            } else {
                $this->error('数据不存在！');
            }
        } else {
            $this->error('数据不存在！');
        }
    }
}
?>